package com.my.mall.client.goods.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.my.mall.client.goods.service.GoodsClientService;
import com.my.mall.client.goods.vo.GoodsCategoryClientVo;
import com.my.mall.client.goods.vo.GoodsClientVo;
import com.my.mall.client.goods.vo.ImageClientVo;
import com.my.mall.common.Base;

@Controller
@RequestMapping("client/goods")
public class GoodsClientController extends Base {

	@Autowired
	GoodsClientService goodsClientService;

	/**
	 * 查询首页商品
	 * @param categoryId
	 * @param pageNumber
	 * @param length
	 * @return
	 */
	@RequestMapping("selectGoodsInfoList/{categoryId}/{pageNumber}/{length}")
	@ResponseBody
	public Map<String, Object> selectGoodsInfoList(@PathVariable("categoryId") int categoryId, @PathVariable("pageNumber") int pageNumber, @PathVariable("length") int length) {
		List<GoodsClientVo> list = goodsClientService.selectGoodsInfoList(categoryId, (pageNumber-1)*length, length);
		int size = goodsClientService.selectGoodsInfoCount(categoryId);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("totalRecords", size);
		map.put("list", list);
		return map;
	}
	
	/**
	 * 查询商品详情
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("getGoodsInfo/{id}")
	@ResponseBody
	public GoodsClientVo getGoodsInfo(@PathVariable("id") int id) {
		GoodsClientVo goodsInfo = goodsClientService.getGoodsInfo(id);
		if(goodsInfo != null) {
			List<ImageClientVo> imgList = goodsClientService.selectGoodsImgList(id);
			goodsInfo.setImgList(imgList);
		}
		return goodsInfo;
	}
	
	/**
	 * 查询商品所有分类
	 * @return
	 */
	@RequestMapping("selectGoodsCategoryList")
	@ResponseBody
	public List<GoodsCategoryClientVo> selectGoodsCategoryList() {
		return goodsClientService.selectGoodsCategoryList();
	}

}